﻿#include "DlgMain.h"
#include "ui_DlgMain.h"

#include "../include/itservice.h"
#include "../include/itcommon.h"
#include "../include/itlogger.h"
#include "../UI/CheckIn/define.h"
#include "../UI/Guide/define.h"
#include "../UI/Consultant/define.h"
#include "../UI/Propagandist/define.h"
#include "../Public/NetComm/define.h"

static void __stdcall fnUIStart(Tai_CB_Msg *pData, void *userdata)
{
    qDebug() << "fnUIStart threadId=" << QThread::currentThreadId();
}

static void __stdcall fnUICheckIn(Tai_CB_Msg *pData, void *userdata)
{
    TUICheckIn_test *param = (TUICheckIn_test*)pData->lParam;
    qDebug() << "fnUICheckIn threadId=" << QThread::currentThreadId() << pData->ulMsg << param->nResult << param->text;
    emit ((DlgMain*)userdata)->sigcheckInTest(param->text);
}

static void __stdcall fnUIConsultant(Tai_CB_Msg *pData, void *userdata)
{
    qDebug() << "fnUIConsultant" << pData->ulMsg;
}

static void __stdcall fnUIGuide(Tai_CB_Msg *pData, void *userdata)
{
    qDebug() << "fnUIGuide" << pData->ulMsg;
}

static void __stdcall fnUIPropagandist(Tai_CB_Msg *pData, void *userdata)
{
    qDebug() << "fnUIPropagandist" << pData->ulMsg;
}

DlgMain::DlgMain(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::DlgMain)
{
    ui->setupUi(this);
    connect(&m_timer,&QTimer::timeout,this,&DlgMain::slotTimer);
    m_nIndex = 0;
    //
    g_pFramework->GetCommon(&m_pcommon);
    //
	g_pFramework->SetCBDlgStart(fnUIStart,this);
    g_pFramework->SetCBCheckIn(fnUICheckIn,this);
    g_pFramework->SetCBConsultant(fnUIConsultant,this);
    g_pFramework->SetCBGuide(fnUIGuide,this);
    g_pFramework->SetCBPropagandist(fnUIPropagandist,this);
    //充分体现在同一个线程里，是直连接，在不同线程，是队列连接；
    connect(this,&DlgMain::sigcheckInTest,this,&DlgMain::slotgcheckInTest);
}

DlgMain::~DlgMain()
{
    m_timer.stop();
    delete ui;
}

void DlgMain::slotgcheckInTest(QString text)
{
    qDebug() << "DlgMain threadId=" << QThread::currentThreadId() << text;
}

void DlgMain::slotTimer()
{
//    on_pushButton_clicked();
//    qDebug() << m_nIndex++;
//    return;
    m_nIndex++;
    if(1 == m_nIndex)
        on_pushButton_clicked();
    else if(2 == m_nIndex)
        on_pushButton_2_clicked();
    else if(3 == m_nIndex)
        on_pushButton_4_clicked();
    else
        m_nIndex = 0;
}

void DlgMain::on_pushButton_clicked()
{
    //同一个模块里，“输入”和获取“结果”
    ITService *pUiService = NULL;
    g_pFramework->GetUiService(&pUiService);
    if(NULL != pUiService)
    {
        TArgs* pTask = NULL;
        pUiService->CreateTask(&pTask,(long)sizeof(TUICheckIn_request_Param),(long)sizeof(TUICheckIn_reply_Param));
        pTask->UiID = UI_CHECKIN_ID;
        pTask->CommandID = UI_CHECKIN_request_CMD_ID;
        TUICheckIn_request_Param* pInCmdParam = (TUICheckIn_request_Param*)pTask->CommandArgs;
        m_pcommon->copystru8("hello I'm CheckIn!", pInCmdParam->send, sizeof(pInCmdParam->send));
        pTask->RstModelID = UI_CHECKIN_ID;
        pTask->RstCommandID = UI_CHECKIN_reply_CMD_ID;
        pUiService->AddTask(pTask);
    }
}

void DlgMain::on_pushButton_2_clicked()
{
    //内存泄漏测试
    if(!m_timer.isActive())
        m_timer.start(100);
}

void DlgMain::on_pushButton_3_clicked()
{
    //"输入" 在一个模块，"结果" 在另一个模块
    ITService *pUiService = NULL;
    g_pFramework->GetUiService(&pUiService);
    if(NULL != pUiService)
    {
        TArgs* pTask = NULL;
        pUiService->CreateTask(&pTask,(long)sizeof(TUIGUIDE_request_Param),(long)sizeof(TUICONSULTANT_reply_Param));
        pTask->UiID = UI_GUIDE_ID;
        pTask->CommandID = UI_GUIDE_request_CMD_ID;
        TUIGUIDE_request_Param* pInCmdParam = (TUIGUIDE_request_Param*)pTask->CommandArgs;
        m_pcommon->copystru8("hello I'm Guide!", pInCmdParam->send, sizeof(pInCmdParam->send));
        pTask->RstModelID = UI_CONSULTANT_ID;
        pTask->RstCommandID = UI_CONSULTANT_reply_CMD_ID;
        pUiService->AddTask(pTask);
    }
}

void DlgMain::on_pushButton_4_clicked()
{
    //在一个模块里，调用另一个模块
    ITService *pUiService = NULL;
    g_pFramework->GetUiService(&pUiService);
    TArgs* pTask = NULL;
    pUiService->CreateTask(&pTask,0,0);
    pTask->UiID = UI_PROPAGANDIST;
    pTask->CommandID = UI_PROPAGANDIST_trans_CMD_ID;
    pUiService->AddTask(pTask);
}

void DlgMain::on_pushButton_5_clicked()
{
    ITService *pSaleswoman = NULL;
    g_pFramework->GetSaleswoman(&pSaleswoman);
    TArgs* pTask = NULL;
    QString text = u8"你该不会是个傻子吧你helloworld";
    pSaleswoman->CreateTask(&pTask,(long)sizeof(TPNetComm_SendRobotCoreBehaviour),0);
    pTask->PublicID = PUBLIC_NETCOMM_ID;
    pTask->CommandID = Public_NetComm_SendRobotCoreBehaviour_CMD_ID;
    TPNetComm_SendRobotCoreBehaviour *param = (TPNetComm_SendRobotCoreBehaviour*)pTask->CommandArgs;
    m_pcommon->copystru8(text, param->wx, sizeof(param->wx));
    param->ndelay = 1112;
    pSaleswoman->AddTask(pTask);
}
